package com.qianqian.service.service;

import com.qianqian.service.mapper.CapitalAccountMapper;
import com.qianqian.service.mapper.TiXianMapper;
import com.qianqian.service.model.CapitalAccount;
import com.qianqian.service.model.CapitalAccountExample;
import com.qianqian.service.model.TiXian;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CapitalAccountService {
    @Autowired
    CapitalAccountMapper capitalAccountMapper;

    @Autowired
    TiXianMapper tiXianMapper;

    private static final int TI_XIAN_TYPE_JIFEN = 2;

    private static final int TI_XIAN_TYPE_YINGLI = 1;


    public CapitalAccount selectByUserId(String userId){
        return capitalAccountMapper.selectByUserId(userId);
    }

    public void updateAccount(CapitalAccount capitalAccount){
        CapitalAccountExample capitalAccountExample = new CapitalAccountExample();
        CapitalAccountExample.Criteria criteria = capitalAccountExample.createCriteria();
        criteria.andUserIdEqualTo(capitalAccount.getUserId());
        capitalAccountMapper.updateByExampleSelective(capitalAccount,capitalAccountExample);
    }

    public void tiXianJifen(String userId,double amount){
        TiXian tiXian = new TiXian();
        tiXian.setAmount(amount);
        tiXian.setCreateTime(new Date());
        tiXian.setIsApproval(0);
        tiXian.setType(TI_XIAN_TYPE_JIFEN);
        tiXian.setUserId(userId);
        tiXianMapper.insertSelective(tiXian);
    }

    public void insertSelective(CapitalAccount record){
        capitalAccountMapper.insertSelective(record);
    }

}
